﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SkyDotNet
{
    /// <summary>
    /// Logique d'interaction pour MainGameWindow.xaml
    /// </summary>
    public partial class MainGameWindow : Window
    {
        public string folderPath = @"..\..\Flag_pictures";

        private bool gameStarted;
        private RadioButton rightRadioButton;

        public MainGameWindow(bool isMultiPlayerMode)
        {
            InitializeComponent();

            setResponsesComboBoxOptions();
            setTimerComboBoxOptions();
            setRadioButtons();

            if (isMultiPlayerMode)
                setMultiPlayerMode();

            gameStarted = false;
        }

        private void setResponsesComboBoxOptions()
        {
            // 3 to 6 responses
            for (int i = 3; i <= 6; i++)
            {
                responsesComboBox.Items.Add(i);
            }

            responsesComboBox.SelectedIndex = 0;
        }

        private void setTimerComboBoxOptions()
        {
            // 3 to 60 seconds
            for (int i = 3; i <= 60; i++)
            {
                timerComboBox.Items.Add(i);
            }

            timerComboBox.SelectedIndex = timerComboBox.Items.Count - 1;
        }

        private void setRadioButtons()
        {
            // C'est moche ça, non ???
            switch((int)responsesComboBox.SelectedItem)
            {
                case 3:
                    RadioButton4.Visibility = Visibility.Hidden;
                    RadioButton5.Visibility = Visibility.Hidden;
                    RadioButton6.Visibility = Visibility.Hidden;
                    break;
                case 4:
                    RadioButton4.Visibility = Visibility.Visible;
                    RadioButton5.Visibility = Visibility.Hidden;
                    RadioButton6.Visibility = Visibility.Hidden;
                    break;
                case 5:
                    RadioButton4.Visibility = Visibility.Visible;
                    RadioButton5.Visibility = Visibility.Visible;
                    RadioButton6.Visibility = Visibility.Hidden;
                    break;
                case 6:
                    RadioButton4.Visibility = Visibility.Visible;
                    RadioButton5.Visibility = Visibility.Visible;
                    RadioButton6.Visibility = Visibility.Visible;
                    break;
            }

            RadioButton1.Content = "";
            RadioButton2.Content = "";
            RadioButton3.Content = "";
            RadioButton4.Content = "";
            RadioButton5.Content = "";
            RadioButton6.Content = "";

            RadioButton1.IsChecked = false;
            RadioButton2.IsChecked = false;
            RadioButton3.IsChecked = false;
            RadioButton4.IsChecked = false;
            RadioButton5.IsChecked = false;
            RadioButton6.IsChecked = false;
        }

        private void setMultiPlayerMode()
        {
            // Add the tchat
        }

        private void displayNewQuestion()
        {
            int responsesNb = (int)responsesComboBox.SelectedItem;
            Question question = QuestionGenerator.Generate(responsesNb, folderPath);


            // Beurk, ça aussi c'est moche
            List<RadioButton> placesAvailable = new List<RadioButton>();
            placesAvailable.Add(RadioButton1);
            placesAvailable.Add(RadioButton2);
            placesAvailable.Add(RadioButton3);

            switch (responsesNb)
            {
                case 4:
                    placesAvailable.Add(RadioButton4);
                    break;
                case 5:
                    placesAvailable.Add(RadioButton4);
                    placesAvailable.Add(RadioButton5);
                    break;
                case 6:
                    placesAvailable.Add(RadioButton4);
                    placesAvailable.Add(RadioButton5);
                    placesAvailable.Add(RadioButton6);
                    break;
            }
            
            // Placement aléatoire des réponses
            Random randomizer = new Random();

            for (int i = 0; i < question.otherResponses.Count() + 1; i++)
            {
                int radioButtonPlace = randomizer.Next(placesAvailable.Count);
                RadioButton radioButton = (RadioButton)placesAvailable.ElementAt(radioButtonPlace);
                placesAvailable.RemoveAt(radioButtonPlace);

                if (i == 0)
                {
                    rightRadioButton = radioButton;
                    radioButton.Content = question.rightResponse;
                }
                else
                {
                    radioButton.Content = question.otherResponses.ElementAt(i - 1);
                }
            }

            loadRightPicture(question.rightResponse);
        }

        private void loadRightPicture(string rightResponse)
        {
            string filepath = "";

            foreach (string completefilename in Directory.GetFiles(folderPath))
            {
                string filename = completefilename.Substring(completefilename.LastIndexOf(@"\") + 1).Split('.').First();

                if (filename.Equals(rightResponse))
                {
                    filepath = completefilename;
                }
            }

            BitmapImage bi3 = new BitmapImage();
            bi3.BeginInit();
            bi3.UriSource = new Uri(filepath, UriKind.Relative);
            bi3.CacheOption = BitmapCacheOption.OnLoad;
            bi3.EndInit();

            imageControl.Source = bi3;
        }

        private void resetGame()
        {
            gameStarted = false;
            mainButton.Content = "Ready";
        }

        #region Events
        private void mainButton_Click(object sender, RoutedEventArgs e)
        {
            if (!gameStarted)
            {
                gameStarted = true;
                mainButton.Content = "Validate";
            }
            else
            {
                //rightRadioButton.Background = Brushes.Green;
            }

            Thread.Sleep(1000); // Pas terrible comme technique pour retarder l'affichage : ça fige toute l'UI

            setRadioButtons();

            displayNewQuestion();
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            setRadioButtons();
            resetGame();
        }

        #endregion
    }
}
